/**
 * @Title: FfmpegCutPic.java
 * @Copyright (C) 2017 龙图软件
 * @Description:
 * @Revision History:
 * @Revision 1.0 2017-11-29  yuanchunci
 */
 

package com.ycc.uitl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: FfmpegCutPic
 * @Description: Description of this class
 * @author <a href="mailto:XXX@qq.com">yuanchunci</a>于 2017-11-29 下午3:07:04
 */

public class FfmpegCutPic{

  public static boolean  cutPic(String ffmpegPath,String upFilePath,String mediaPicPath) throws IOException{
			List cutpic = new ArrayList(); 
			cutpic.add(ffmpegPath); // 视频提取工具的位置
			cutpic.add("-i"); // 添加参数＂-i＂，该参数指定要转换的文件 
			cutpic.add(upFilePath.trim()); // 视频文件路径 
			cutpic.add("-y"); 
			cutpic.add("-f"); 
			cutpic.add("image2"); 
			cutpic.add("-ss"); // 添加参数＂-ss＂，该参数指定截取的起始时间
			cutpic.add("1"); // 添加起始时间为第1秒 
			cutpic.add("-t"); // 添加参数＂-t＂，该参数指定持续时间 
			cutpic.add("0.001"); // 添加持续时间为1毫秒 
			cutpic.add("-s"); // 添加参数＂-s＂，该参数指定截取的图片大小 
			cutpic.add("125*125"); // 添加截取的图片大小为800*600 
			cutpic.add(mediaPicPath); // 添加截取的图片的保存路径
	        boolean mark = true;  
//	        ProcessBuilder builder = new ProcessBuilder();  
	        try {  
	        Runtime runtime = Runtime.getRuntime();
	        Process proce = null;
	        String cmd = "";
	        String cut = "  "+ffmpegPath+"   -i   " + upFilePath.trim()
	                             + "   -y   -f   image2   -ss   1   -t   0.001   -s   125*125  " + mediaPicPath;
	        String cutCmd = cmd + cut;
	        proce = runtime.exec(cutCmd);
	        ProcessBuilder builder = new ProcessBuilder(cutpic);
	        builder.command(cutpic);
	            // 如果此属性为 true，则任何由通过此对象的 start() 方法启动的后续子进程生成的错误输出都将与标准输出合并，  
	            //因此两者均可使用 Process.getInputStream() 方法读取。这使得关联错误消息和相应的输出变得更容易  
	         builder.start();  
	         builder.redirectErrorStream(true);

	       //获取进程的标准输入流  
	         final InputStream is1 = proce.getInputStream();   
	         //获取进城的错误流  
	         final InputStream is2 = proce.getErrorStream();  
	         //启动两个线程，一个线程负责读标准输出流，另一个负责读标准错误流  
	         new Thread() {  
	            public void run() {  
	               BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));  
	                try {  
	                    String line1 = null;  
	                    while ((line1 = br1.readLine()) != null) {  
	                          if (line1 != null){}  
	                      }  
	                } catch (IOException e) {  
	                     e.printStackTrace();  
	                }  
	                finally{  
	                     try {  
	                       is1.close();  
	                     } catch (IOException e) {  
	                        e.printStackTrace();  
	                    }  
	                  }  
	                }  
	             }.start();  
	                                        
	           new Thread() {   
	              public void  run() {   
	               BufferedReader br2 = new  BufferedReader(new  InputStreamReader(is2));   
	                  try {   
	                     String line2 = null ;   
	                     while ((line2 = br2.readLine()) !=  null ) {   
	                          if (line2 != null){}  
	                     }   
	                   } catch (IOException e) {   
	                         e.printStackTrace();  
	                   }   
	                  finally{  
	                     try {  
	                         is2.close();  
	                     } catch (IOException e) {  
	                         e.printStackTrace();  
	                     }  
	                   }  
	                }   
	              }.start();    
	                                        
	              proce.waitFor();  
	              proce.destroy();  
	         System.out.println("ffmpeg.exe运行完毕！"); 
	        } catch (Exception e) {  
	            mark = false;  
	            System.out.println(e);  
	            e.printStackTrace();  
	        }  
	        return mark;//mediaPicPath;
  }
  
  
  public static void main(String[] args) throws IOException{
	  System.out.println(System.getenv("Path"));
	  String cmd=null;
	  String[] str=System.getenv("Path").split(";");
	  for(int i=0;i<str.length;i++){
		  if(str[i].toLowerCase().indexOf("ffmpeg")>-1){
			  cmd=str[i]+"\\ffmpeg.exe";
			  break;
		  }
	  }
	  System.out.println(cmd);
	  if(cmd==null){
		  System.out.println("没有配置环境变量");
		  return;
	  }
	  cutPic(cmd,"C:/upload/f2047b07076b4fada39df1fbde361878QQ20171009140809.mp4","C:/upload/thum/f2047b07076b4fada39df1fbde361878QQ20171009140809.jpg"); 
	  System.exit(0);
     } 
}
